home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / bin / ods-server < prev    next >
Text File  |  2009-09-07  |  8KB  |  187 lines

  1. #!/usr/bin/python
  2.  
  3. import dbus
  4. import dbus.decorators
  5. import dbus.glib
  6. import gobject
  7. from optparse import OptionParser
  8. import sys
  9. from signal import *
  10. import time
  11.  
  12. class ServerSession:
  13.     
  14.     def __init__(self, session_object_path):
  15.         self.prefix = '>>'+session_object_path+'<< '
  16.         self.bus = dbus.SessionBus()
  17.         
  18.         # get org.openobex.ServerSession object
  19.         session_obj = self.bus.get_object('org.openobex', session_object_path)
  20.         self.session = dbus.Interface(session_obj, 'org.openobex.ServerSession')
  21.         
  22.         # connect signals
  23.         self.session.connect_to_signal('Disconnected', self.disconnected_cb)
  24.         self.session.connect_to_signal('Cancelled', self.cancelled_cb)
  25.         self.session.connect_to_signal('TransferStarted', self.transfer_started_cb)
  26.         self.session.connect_to_signal('TransferProgress', self.transfer_progress_cb)
  27.         self.session.connect_to_signal('TransferCompleted', self.transfer_completed_cb)
  28.         self.session.connect_to_signal('ErrorOccurred', self.error_occurred_cb)
  29.         self.session.connect_to_signal('RemoteDisplayRequested', self.remote_display_requested_cb)
  30.     
  31.     # emitted when session is disconnected
  32.     def disconnected_cb(self):
  33.         print self.prefix,
  34.         print 'Disconnected'
  35.     
  36.     # emitted when transfer is cancelled
  37.     def cancelled_cb(self):
  38.         print self.prefix,
  39.         print 'Transfer cancelled'
  40.  
  41.     # emitted when transfer begins
  42.     def transfer_started_cb(self, filename, local_path, total_bytes):
  43.         print self.prefix,
  44.         print 'Transfer started (%s, %s, %d)' % (filename, local_path, total_bytes)
  45.         self.total_bytes = total_bytes
  46.         
  47.         info = self.session.GetTransferInfo()
  48.         print self.prefix, 'All transfer info:'
  49.         for name,value in info.iteritems():
  50.             print self.prefix, '--', name, '=', value
  51.         
  52.         if options.ask_to_accept:
  53.             print "Accept file? Type 'a' to accept, 'r' to reject:"
  54.             command = raw_input('>>> ')
  55.             if command.strip() == 'a':
  56.                 print 'Accepting'
  57.                 self.session.Accept()
  58.             else:
  59.                 print 'Rejecting'
  60.                 self.session.Reject()   
  61.     
  62.     # emitted constantly during transfer
  63.     def transfer_progress_cb(self, bytes_transferred):
  64.         print self.prefix,
  65.         if self.total_bytes > 0:
  66.             print 'Progress: %d %%' % int(float(bytes_transferred)/self.total_bytes*100)
  67.         else:
  68.             print 'Progress'
  69.     
  70.     # emitted when transfer is completed
  71.     def transfer_completed_cb(self):
  72.         print self.prefix,
  73.         print 'Transfer completed'
  74.     
  75.     # emitted when error occurs (for instance link error)
  76.     def error_occurred_cb(self, error_name, error_message):
  77.         print self.prefix,
  78.         print 'Error occurred: %s: %s' % (error_name, error_message)
  79.         
  80.     # emitted when BIP RemoteDisplay feature is requested
  81.     def remote_display_requested_cb(self, img_filename):
  82.            print self.prefix,
  83.            print 'RemoteDisplay requested for image: ', img_filename
  84.  
  85.  
  86. class Tester:
  87.     
  88.     def __init__(self, server_type, root_path, options):
  89.         self.server_type = server_type
  90.         self.root_path = root_path
  91.         self.options = options
  92.         
  93.         # get Session bus
  94.         self.bus = dbus.SessionBus()
  95.         
  96.         # get org.openobex.Manager object
  97.         manager_obj = self.bus.get_object('org.openobex', '/org/openobex')
  98.         self.manager = dbus.Interface(manager_obj, 'org.openobex.Manager')
  99.         
  100.         # call CreateBluetoothServer with specified server type 
  101.         # (opp - Object Push Profile,
  102.         #  ftp - File Transfer Profile,
  103.         #  bip - Basic Imaging Profile)
  104.         # returns Server object path
  105.         server_path = self.manager.CreateBluetoothServer(options.local_device,
  106.                                              self.server_type, options.pairing)
  107.         print 'Server object: ', server_path
  108.         # get org.openobex.Server object
  109.         server_obj = self.bus.get_object('org.openobex', server_path)
  110.         self.server = dbus.Interface(server_obj, 'org.openobex.Server')
  111.  
  112.         # connect signals
  113.         self.server.connect_to_signal('Started', self.started_cb)
  114.         self.server.connect_to_signal('Stopped', self.stopped_cb)
  115.         self.server.connect_to_signal('Closed', self.closed_cb)
  116.         self.server.connect_to_signal('ErrorOccurred', self.error_occurred_cb)
  117.         self.server.connect_to_signal('SessionCreated', self.session_created_cb)
  118.         self.server.connect_to_signal('SessionRemoved', self.session_removed_cb)
  119.         
  120.         # require remote device to send thumbnails for BIP PutImage sessions
  121.         self.server.SetOption('RequireImagingThumbnails', options.thumbnails)
  122.         # start server with specified options
  123.         self.server.Start(self.root_path, not options.readonly, not options.ask_to_accept)
  124.         
  125.         self.main_loop = gobject.MainLoop()
  126.         self.main_loop.run()
  127.     
  128.     # emitted when Server is started
  129.     def started_cb(self):
  130.         print 'Started'
  131.     
  132.     # emitted when Server is stopped
  133.     def stopped_cb(self):
  134.         print 'Stopped'
  135.     
  136.     # emitted when Server is closed
  137.     def closed_cb(self):
  138.         print 'Closed'
  139.     
  140.     def error_occurred_cb(self, error_name, error_message):
  141.         print 'Error occurred: %s: %s' % (error_name, error_message)
  142.     
  143.     # emitted when client connects to server (server session is established)    
  144.     def session_created_cb(self, session_object_path):
  145.         print 'Session created: %s' % session_object_path
  146.     session_info = self.server.GetServerSessionInfo(session_object_path)
  147.     print 'Session Bluetooth address: %s' % session_info['BluetoothAddress']
  148.         session = ServerSession(session_object_path)
  149.     
  150.     # emitted when client disconnects
  151.     def session_removed_cb(self, session_object_path):
  152.         print 'Session removed: %s' % session_object_path
  153.  
  154. if __name__ == '__main__':
  155.     gobject.threads_init()
  156.     dbus.glib.init_threads()
  157.  
  158.     usage = 'Usage: '+sys.argv[0]+' [options] profile path'
  159.     parser = OptionParser(usage)
  160.     parser.add_option('-l', '--local', dest='local_device',
  161.                       default='00:00:00:00:00:00',
  162.                       help='ADDRESS of Bluetooth adapter to listen on. Default is 00:00:00:00:00:00',
  163.                       metavar='ADDRESS')
  164.     parser.add_option('-p', '--pairing', dest='pairing',
  165.                       action='store_true', default=False,
  166.                       help='Require remote devices to be paired before allowing them to connect. Disabled by default')
  167.     parser.add_option('-r', '--readonly', dest='readonly',
  168.                       action='store_true', default=False,
  169.                       help='Disallow any write operations. Allowed by default')
  170.     parser.add_option('-t', '--thumbnails', dest='thumbnails',
  171.                       action='store_true', default=False,
  172.                       help='Require remote device to send thumbnails when using Imaging (BIP) server')
  173.     parser.add_option('-a', '--ask-to-accept', dest='ask_to_accept',
  174.                       action='store_true', default=False,
  175.                       help='Prompt user to accept or reject every file. By default all files are accepted')
  176.                       
  177.     options, args = parser.parse_args()
  178.     
  179.     if len(args) != 2:
  180.         print usage
  181.         print
  182.         print 'error: Wrong number of arguments'
  183.         exit()
  184.     
  185.     tester = Tester(args[0], args[1], options)
  186.     
  187.